package hadoop.mapreduce.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * 自定义reducer阶段写出约束类
 */
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    String atguiguOutPath="C:\\tmp\\aaa\\atguigu.txt";
    String otherOutPath="C:\\tmp\\aaa\\other.txt";

    private FileSystem fs;
    private FSDataOutputStream atguiguOut;
    private FSDataOutputStream otherOut;


    public LogRecordWriter(TaskAttemptContext job) {
        try {
            //获取文件系统对象
            fs = FileSystem.get(job.getConfiguration());
            //用文件系统对象创建两个输出流对应不同的目录
            atguiguOut = fs.create(new Path(atguiguOutPath));
            otherOut = fs.create(new Path(otherOutPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * reducer阶段的输出
     * @param key
     * @param value
     * @throws IOException
     * @throws InterruptedException
     */
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String log = key.toString();
        if (log.contains("atguigu")){
            atguiguOut.writeBytes(log+"\n");
        }else {
            otherOut.writeBytes(log+"\n");
        }
    }

    /**
     * 关闭资源
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        //关流
        IOUtils.closeStream(atguiguOut);
        IOUtils.closeStream(otherOut);
        fs.close();
    }

}
